#!/bin/bash
root_dir="/usr/local/edh"
script_path=$(readlink -f "${BASH_SOURCE[0]}")
script_dir=$(dirname "$script_path")
if [ ! -d "$root_dir" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir undetected"
  mkdir -p $root_dir
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO creating $root_dir success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR creating $root_dir fail"
    return
  fi
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir detected"
fi
MODE_FILE="$script_dir/mode"
if [[ ! -e "$MODE_FILE" ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR hadoop mode is empty"
  return
fi

jdk_ver=$(java -version 2>&1)
if [[ $jdk_ver == *"not found"* ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING jdk is not found,please install jdk first"
  return
else
  echo $jdk_ver
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO jdk detected"
fi

if [ ! -d "$root_dir/hadoop" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir/hadoop undetected"
  tar -xzvf "$script_dir/hadoop-3.4.1.tar.gz" -C "$root_dir"
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO unzip $$script_dir/hadoop-3.4.1.tar.gz success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR unzip $$script_dir/hadoop-3.4.1.tar.gz fail"
    return
  fi
  mv "$root_dir/hadoop-3.4.1" "$root_dir/hadoop"
  mkdir -p $root_dir/hadoop/hdfs/data
  mkdir -p $root_dir/hadoop/hdfs/name
  mkdir -p $root_dir/hadoop/hdfs/journal
  mkdir -p $root_dir/hadoop/tmp
  env_file="$script_dir/config.env"
  conf_dir=$root_dir/hadoop/etc/hadoop
  while IFS= read -r line
  do
    if [[ -n "$line" ]];then
      IFS="_" read -r prefix suffix <<< "$line"
      IFS="=" read -r name value <<< "$suffix"
      echo "$(date +"%Y-%m-%d %H:%M:%S") setting $name=$value"
      entry="<property><name>$name</name><value>$value</value></property>"
      entry_escaped=$(echo $entry | sed 's/\//\\\//g')
      if [ $prefix == "CORE" ];then
        sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/core-site.xml
      fi
      if [ $prefix == "HDFS" ];then
        sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/hdfs-site.xml
      fi
      if [ $prefix == "YARN" ];then
        sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/yarn-site.xml
      fi
      if [ $prefix == "MAPRED" ];then
        sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/mapred-site.xml
      fi
      if [ $prefix == "WORKER" ];then
        rm $conf_dir/workers
        echo -e "$value" >> $conf_dir/workers
      fi
    fi
  done < "$env_file"
  echo "export HADOOP_HOME=$root_dir/hadoop" >> ~/.bashrc
  echo 'export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH' >> ~/.bashrc
  echo "export HADOOP_HOME=$root_dir/hadoop" >> /etc/profile
  echo 'export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH' >> /etc/profile
  source ~/.bashrc
  source /etc/profile
  hdfs_ver=$(hdfs version 2>&1)
  echo $hdfs_ver
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir/hadoop detected"
  source ~/.bashrc
  source /etc/profile
  hdfs_ver=$(hdfs version 2>&1)
  echo $hdfs_ver
fi





